﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Extensions;  
using System.Web.Security;

namespace ERPStore.Tests
{
	public static class TestHelper
	{
		public static ERPStore.Models.UserPrincipal GetCustomerUserPrincipal()
		{
			var accountService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IAccountService>();
			var user = accountService.GetUserById(1);
			return user.GetCustomerUserPrincipal();
		}

		public static ERPStore.Models.UserPrincipal GetCustomerUserPrincipal(this ERPStore.Models.User user)
		{
			var ticket = new FormsAuthenticationTicket("mock", false, 100);
			var id = new FormsIdentity(ticket);
			var userPrincipal = new ERPStore.Models.UserPrincipal(id, Guid.NewGuid().ToString());
			userPrincipal.CurrentUser = user;
			user.Roles.Add("customer");
			return userPrincipal;
		}

		public static ERPStore.Models.User GetDefaultConnectedUser()
		{
			var accountService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IAccountService>();
			var userId = Convert.ToInt32(ERPStore.Configuration.ConfigurationSettings.AppSettings["defaultConnectedUserId"]);
			var user = accountService.GetUserById(userId);
			return user;
		}

		public static string GetRandomString(int length = 10)
		{
			return Guid.NewGuid().ToString().Replace("-", "").Substring(0, length);
		}

		public static string GetFakeEmail()
		{
			return GetRandomString() + "@test.com";
		}

		public static string GetRandomPhoneNumber(int count = 10)
		{
			var random = new Random();
			var result = "0";
			for (int i = 0; i < count - 1; i++)
			{
				var number = random.Next(9);
				result += number.ToString();
			}
			return result;
		}

		public static void FillRandomRegistrationUser(this ERPStore.Models.RegistrationUser registrationUser)
		{
			registrationUser.PresentationId = (int)ERPStore.Models.UserPresentation.Mister;
			registrationUser.FirstName = "F-" + GetRandomString();
			registrationUser.LastName = "L-" + GetRandomString();
			registrationUser.MobileNumber = GetRandomPhoneNumber(10);
			registrationUser.Password = GetRandomString(8);
			registrationUser.PhoneNumber = GetRandomPhoneNumber(10);
			registrationUser.FaxNumber = GetRandomPhoneNumber(10);
			registrationUser.Email = GetFakeEmail();
		}

		public static void FillRandomRegistrationUserAddress(this ERPStore.Models.RegistrationUser registrationUser)
		{
			registrationUser.ShippingAddressRecipientName = registrationUser.FullName;
			registrationUser.ShippingAddressCity = GetRandomString(15);
			registrationUser.ShippingAddressCountryId = ERPStore.Models.Country.Default.Id;
			registrationUser.ShippingAddressStreet = GetRandomString(15);
			registrationUser.ShippingAddressZipCode = GetRandomPhoneNumber(5);
		}

	}
}
